package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IPublicationMediaListByRegionForManagerView;
import com.cloudsigns.factorymanager2.engineering.IView.InRegionListView;
import com.cloudsigns.factorymanager2.engineering.adapter.DropDownPoupWindowAdapter;
import com.cloudsigns.factorymanager2.engineering.adapter.TableContentEngineeringInstallForManagerAdapter;
import com.cloudsigns.factorymanager2.engineering.adapter.TableLeftEngineeringInstallForManagerAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.PublicationOrderDetailBean;
import com.cloudsigns.factorymanager2.engineering.bean.RegionBean;
import com.cloudsigns.factorymanager2.engineering.customview.SpinerPopWindow;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationMediaListByRegionForManagerPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.RegionListPresenter;
import com.cloudsigns.factorymanager2.project.customview.NoscrollListView;
import com.cloudsigns.factorymanager2.project.customview.SyncHorizontalScrollView;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tony on 2018/7/10.
 */

public class PublicationStatusTableForManagerActivity2 extends Activity implements IPublicationMediaListByRegionForManagerView, InRegionListView, View.OnClickListener, AdapterView.OnItemClickListener, ItemClickListener {
    private RelativeLayout rl_right;
//    private ImageView img_right;
    private TextView tv_title;
    private ViewGroup rl_back;
    private TextView tv_head_title;
    private View rl_progressbar;
    private SyncHorizontalScrollView mHeaderHorizontal;
    private SyncHorizontalScrollView mDataHorizontal;

    /*表头 日期*/
    private LinearLayout ll_year_title;
    private LinearLayout ll_left;
    private NoscrollListView lvData;

    private TableContentEngineeringInstallForManagerAdapter mDataAdapter;
    private LayoutInflater layoutInflater;
    private String TAG = PublicationStatusTableForManagerActivity2.class.getSimpleName();

    RegionListPresenter regionListPresenter;
    private List<RegionBean> regionDataList = new ArrayList<RegionBean>();

    private PublicationMediaListByRegionForManagerPresenter publicationMediaListByRegionForManagerPresenter;
    private List<PublicationOrderDetailBean> publicationOrderDetailBeanList = new ArrayList<>();
    List<String> tableHeadDataList = new ArrayList<>();
    private List<MediaBean> tableContentDataList = new ArrayList<>();

    private SpinerPopWindow<String> popwindow;
    private DropDownPoupWindowAdapter popwindowAdapter;
    private String selectedRegionId = "";
    //PublicationStatusTableOrderInfoActivity


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_publication_status_table_for_manager);

        publicationMediaListByRegionForManagerPresenter = new PublicationMediaListByRegionForManagerPresenter(this);
        regionListPresenter = new RegionListPresenter(this);
        init();
        regionListPresenter.getMediaRegionList();

    }

    private void init() {
        rl_right = findViewById(R.id.rl_right);
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
      //  img_right = findViewById(R.id.img_right);

        rl_progressbar = findViewById(R.id.rl_progressbar);

        tv_title.setText("上刊状态表");
       // img_right.setBackgroundResource(R.mipmap.title_icon_menu);
        rl_right.setVisibility(View.VISIBLE);
        rl_back.setOnClickListener(this);
        rl_right.setOnClickListener(this);

        tv_head_title = findViewById(R.id.tv_head_title);
//        lvLeft = (NoscrollListView) findViewById(R.id.lv_left);
        lvData = (NoscrollListView) findViewById(R.id.lv_data);


        mDataHorizontal = (SyncHorizontalScrollView) findViewById(R.id.data_horizontal);
        mHeaderHorizontal = (SyncHorizontalScrollView) findViewById(R.id.header_horizontal);

        ll_year_title = findViewById(R.id.ll_year_title);
        ll_left = findViewById(R.id.ll_left);

        /*实现两个scrollview联动*/
        mDataHorizontal.setScrollView(mHeaderHorizontal);
        mHeaderHorizontal.setScrollView(mDataHorizontal);


        mDataAdapter = new TableContentEngineeringInstallForManagerAdapter(this, TAG);
        mDataAdapter.setData(tableContentDataList);
        mDataAdapter.setHeadData(tableHeadDataList);
        mDataAdapter.setOnclickListener(this);

        lvData.setAdapter(mDataAdapter);

        popwindow = new SpinerPopWindow<String>(this, this);
        popwindowAdapter = new DropDownPoupWindowAdapter(this, TAG);
        popwindowAdapter.setData(regionDataList);
        popwindow.setAdapter(popwindowAdapter);

        layoutInflater = getLayoutInflater();
    }

    private void initData() {


        if (null != publicationOrderDetailBeanList) {
            ll_left.removeAllViews();
            tableContentDataList.clear();
            tableHeadDataList.clear();

            /*表头*/
            long minDate = -1;
//          long maxDate = -1;
            long maxDate = System.currentTimeMillis();


            for (int i = 0; i < publicationOrderDetailBeanList.size(); i++) {

                PublicationOrderDetailBean publicationOrderDetailBean = publicationOrderDetailBeanList.get(i);

                 /*表格数据*/
                MediaBean mediaBean = new MediaBean();
                mediaBean.setPContent(true);
                tableContentDataList.add(mediaBean);


                List<MediaBean> mediaBeanList = publicationOrderDetailBean.getMediaList();
                if (null != mediaBeanList) {
                    for (MediaBean media : mediaBeanList) {
                        media.setOrderId(publicationOrderDetailBean.getId());
                        tableContentDataList.add(media);

                        if (minDate == -1) {
                            minDate = mediaBean.getFeedbackDate();
                        } else {
                            long date = mediaBean.getFeedbackDate();
                            if (date > 0 && date < minDate)
                                minDate = date;
                        }

//                        if (maxDate == -1) {
//                            maxDate = mediaBean.getFeedbackDate();
//                        } else {
//                            long date = mediaBean.getFeedbackDate();
//                            if (date > 0 && date > maxDate)
//                                maxDate = date;
//                        }
                    }

                    if (minDate <= 0) {
                        minDate = maxDate;
                    }

                    publicationOrderDetailBean.setMediaList(mediaBeanList);


                }


                /*表格左边ui*/
                View view = layoutInflater.inflate(R.layout.item_table_left_lv, null);
                TextView tv_pcontent = view.findViewById(R.id.tv_pcontent);
                NoscrollListView lvLeft = view.findViewById(R.id.lv_left_data);

                tv_pcontent.setText(publicationOrderDetailBean.getPublicationContent());
                tv_pcontent.setTag(publicationOrderDetailBean);
                tv_pcontent.setOnClickListener(this);

                TableLeftEngineeringInstallForManagerAdapter adapter = new TableLeftEngineeringInstallForManagerAdapter(this, TAG);
                adapter.setData(publicationOrderDetailBean.getMediaList());
                adapter.setOnclickListerner(this);
                lvLeft.setAdapter(adapter);

                ll_left.addView(view);


            }

            if (minDate > 0 && maxDate > 0) {
                String m = DateUtil.getYyyy_Mm_Dd(minDate);
                minDate = DateUtil.getMillis(m);
//                    long currentDate = System.currentTimeMillis();
                int days = DateUtil.getBetweenDays(minDate, maxDate);
                if (days >= 0) {

                    if (days < 6)
                        days = 6;


                    for (int i = 0; i <= days; i++) {
                        long nextDate = minDate + i * 24 * 60 * 60 * 1000;
                        tableHeadDataList.add(DateUtil.getYyyy_Mm_Dd(nextDate));
                    }
                }
            }
            updateTableHeadUi();


             /*初始化表格内容*/
            mDataAdapter.setData(tableContentDataList);
            mDataAdapter.setHeadData(tableHeadDataList);
            mDataAdapter.notifyDataSetChanged();


        }

    }


    /**
     * 初始化表头
     */
    private void updateTableHeadUi() {
        ll_year_title.removeAllViews();

        if (tableHeadDataList.size() > 0) {
            for (String date : tableHeadDataList) {
                View view = layoutInflater.inflate(R.layout.item_table_head_week_patrol, null);
                TextView tv_week_head = view.findViewById(R.id.tv_week_head);
                TextView tv_year_head = view.findViewById(R.id.tv_year_head);
//                TextView tv_date_head = view.findViewById(R.id.tv_date_head);
                tv_year_head.setText(date.substring(5, date.length()));
                tv_week_head.setText(DateUtil.getWeekByDateStr(date));
                ll_year_title.addView(view);
            }
        }
    }

    private void clearUi() {
        ll_left.removeAllViews();
        tableContentDataList.clear();
        tableHeadDataList.clear();

        updateTableHeadUi();

        mDataAdapter.setData(tableContentDataList);
        mDataAdapter.setHeadData(tableHeadDataList);
        mDataAdapter.notifyDataSetChanged();
    }

    private void showportPop(View anchor) {
        if (null != popwindow)
            popwindow.dismiss();

        popwindow.setWidth(anchor.getWidth() * 2);
//        popwindow.showAsDropDown(anchor);
        popwindow.showAsDropDown(anchor, anchor.getLayoutParams().width / 2, 5);
    }


    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.rl_right:
                showportPop(rl_right);
                break;
            case R.id.tv_pcontent: //上刊内容
                break;


        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//        Intent mIntent;
//        if (adapterView == lvLeft) {
//            mIntent = new Intent(PublicationStatusTableForManagerActivity.this, ViewEngineeringInstallOrderDetailActivity.class);
//            mIntent.putExtra(ConstantUtil.KEY_1, TAG);
//            mIntent.putExtra(ConstantUtil.KEY_2, tableLeftDataList.get(i));
//            startActivity(mIntent);
//        } else {
        if (i < regionDataList.size()) {
            if (null != popwindow)
                popwindow.dismiss();

            RegionBean regionBean = regionDataList.get(i);
            if (!regionBean.getId().equals(selectedRegionId)) {
                selectedRegionId = regionBean.getId();
                tv_head_title.setText(regionBean.getRegionName());

                clearUi();
                publicationMediaListByRegionForManagerPresenter.publicationMediaListByRegionForManager();
            }

        }
//        }

    }

    @Override
    public void itemClick(View view, int position) {
        Intent mIntent;
        MediaBean mediaBean;
        switch (view.getId()) {
            case R.id.rl_item_status:
                mediaBean = (MediaBean) view.getTag();
                if (null != mediaBean) {

                    CommonEnum.MediaStatus mediaStatus = mediaBean.getStatus();
                    if (null != mediaStatus) {
//                        if(mediaStatus == CommonEnum.MediaStatus.PROBLEM_APPEAR || mediaStatus == CommonEnum.MediaStatus.PROBLEM_FEEDBACK
//                                || mediaStatus == CommonEnum.MediaStatus.COMPLETE_FEEDBACK){
                        mIntent = new Intent(PublicationStatusTableForManagerActivity2.this, FeedBackSurePublicationForManagerActivity.class);

                        mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                        mIntent.putExtra(ConstantUtil.KEY_2, mediaBean);
                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);

//                        }
                    }


                }
                break;
            case R.id.tv_left:
                mediaBean = (MediaBean) view.getTag();
                if (null != mediaBean) {
                    mIntent = new Intent(PublicationStatusTableForManagerActivity2.this, MediaDetailForPublicationActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                    mIntent.putExtra(ConstantUtil.KEY_2, mediaBean);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                break;
        }


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != data) {
            switch (requestCode) {
                case ConstantUtil.START_ACTIVITY_KEY_1:
                    publicationMediaListByRegionForManagerPresenter.publicationMediaListByRegionForManager();
                    break;
            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (popwindow != null) {
            popwindow.dismiss();
            popwindow = null;
        }

    }

    @Override
    public String getRegionId() {
        return selectedRegionId;
    }

    @Override
    public void publicationMediaListByRegionForManagerSuccessed(List<PublicationOrderDetailBean> publicationOrderDetailBeanList) {
        if (null != publicationOrderDetailBeanList) {
            this.publicationOrderDetailBeanList.clear();
            this.publicationOrderDetailBeanList.addAll(publicationOrderDetailBeanList);

            initData();
        }
    }

    @Override
    public void publicationMediaListByRegionForManagerFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getMediaRegionListSuccessed(List<RegionBean> regionBeanList) {
        if (null != regionBeanList) {
            this.regionDataList.clear();
            this.regionDataList.addAll(regionBeanList);

            popwindowAdapter.setData(regionDataList);
            popwindowAdapter.notifyDataSetChanged();

            if (regionBeanList.size() > 0) {

                RegionBean regionBean = regionDataList.get(0);
                selectedRegionId = regionBean.getId();
                tv_head_title.setText(regionBean.getRegionName());

                publicationMediaListByRegionForManagerPresenter.publicationMediaListByRegionForManager();
            }
        }


    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {
        rl_progressbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        rl_progressbar.setVisibility(View.GONE);
    }

}
